package com.zhdl.modules.assemblybolt.web.controller;

import com.project.common.util.DelayUtil;
import com.zhdl.common.util.WebResult;
import com.zhdl.modules.assemblybolt.KukaRobotProcess;
import com.zhdl.modules.assemblybolt.TransportScrewNextProcess;
import com.zhdl.modules.assemblybolt.service.AssemblyBoltService;
import com.zhdl.modules.tools.CommonCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping(path = "/assemblyBoltRobot", produces = MediaType.APPLICATION_JSON_VALUE)
public class AssemblyBoltController {

    @Resource
    AssemblyBoltService assemblyBoltService;

    @Resource
    KukaRobotProcess kukaRobotProcess;

    @Resource
    TransportScrewNextProcess transportScrewNextProcess;

    /**
     * 移动固定托盘
     *
     * @return 返回结果
     */
    @GetMapping("/regular")
    public WebResult<String> regular() {
        assemblyBoltService.plcControlMotorRunY(1,50.f,225.0f);
        assemblyBoltService.plcControlMotorSerRun(1,true);
        return WebResult.okResult("移动固定托盘!");
    }

    /**
     * 移动到拍照位
     *
     * @return 返回结果
     */
    @GetMapping("/photograph")
    public WebResult<String> photograph() {
        transportScrewNextProcess.plcMotorRun(5, 200, -95.0f, s -> {
            DelayUtil.delayedExecution(2000, () -> {
                transportScrewNextProcess.plcMotorRun(4, 200, 260.1f, s1 -> {
                    log.info("");
                });
            });
        });
        return WebResult.okResult("移动到拍照位!");
    }

    @GetMapping("/bore")
    public WebResult<String> bore() {
        transportScrewNextProcess.plcMotorRun(4, 200, 342.6f, s -> {
            DelayUtil.delayedExecution(2000, () -> {
                transportScrewNextProcess.plcMotorRun(5, 200, -140.0f, s1 -> {

                });
            });
        });
        return WebResult.okResult("移动到打孔位!");
    }

    @GetMapping("/cameraResult")
    public WebResult<String> cameraResult(float tag, float angle, float distance) {
        transportScrewNextProcess.cameraResult(tag,angle,distance);
        return WebResult.okResult("设置识别角度!"+tag+"::::"+angle+"::::::"+distance);
    }
    @GetMapping("/runY")
    public WebResult<String> runY() {
        assemblyBoltService.plcControlMotorRunY(1,10.0f,225.0f);
        assemblyBoltService.plcControlMotorSerRun(1,true);
        return WebResult.okResult("流程开始启动!");
    }

    @GetMapping("/start")
    public WebResult<String> processStart() {
        transportScrewNextProcess.startProcess();
        return WebResult.okResult("流程开始启动!");
    }

    @GetMapping("/axisControl")
    public WebResult<String> axisControl(int axis, float distance) {
        assemblyBoltService.plcControlMotorAbsRun(axis, false);
        assemblyBoltService.plcControlMotorRunX(axis, 200, distance);
        assemblyBoltService.plcControlMotorAbsRun(axis, true);
        return WebResult.okResult("");
    }

    @GetMapping("/robot")
    public WebResult<String> controlRobot() {
        kukaRobotProcess.robotStartProcess();
        return WebResult.okResult("");
    }


    @GetMapping("/writeMotorValue")
    public WebResult<String> plcControlMotorRun(int model, int speed, boolean status, boolean open) {
        assemblyBoltService.plcControlMotorJobRun(model, 12 * 1.0f, status, open);
        return WebResult.okResult("PLC control motor is " + model + ",job run speed is " + speed);
    }

    @GetMapping("/testDown")
    public WebResult<String> testDown(int model) {
        assemblyBoltService.plcControlMotorJobRun(model, 12.0f, false, true);
        controlMicroMotorRun(1,3);
        controlMicroMotorRun(1,1);
        return WebResult.okResult("PLC control motor is " + model + ",job run speed is " + 12);
    }

    @GetMapping("/testUp")
    public WebResult<String> testUp(int model) {
        assemblyBoltService.plcControlMotorJobRun(model, 2.0f, true, true);
        controlMicroMotorRun(1,3);
        controlMicroMotorRun(1,2);
        return WebResult.okResult("PLC control motor is " + model + ",job run speed is " + 12);
    }
    @GetMapping("/testPress")
    public WebResult<String> testPress() {
        assemblyBoltService.plcControlMotorRunY(5,6.0f,-18);
        assemblyBoltService.plcControlMotorSerRun(5,true);
        controlMicroMotorRun(1,3);
        controlMicroMotorRun(1,2);
        DelayUtil.delayedExecution(3000, new Runnable() {
            @Override
            public void run() {
                controlMicroMotorRun(1,4);
            }
        });
        return WebResult.okResult("PLC control motor is " + 5 + ",job run speed is " + 12);
    }
    @GetMapping("/stopPress")
    public WebResult<String> stopPress() {
        assemblyBoltService.plcControlMotorSerRun(5,false);
        controlMicroMotorRun(1,4);
        return WebResult.okResult("PLC control motor is " + 5 + ",job run speed is " + 12);
    }
    @GetMapping("/testUpStop")
    public WebResult<String> testUpStop(int model) {
        assemblyBoltService.plcControlMotorJobRun(model, 12.0f, true, false);
        controlMicroMotorRun(1,4);
        return WebResult.okResult("PLC control motor is " + model + ",job run speed is " + 12);
    }

    @GetMapping("/testDownStop")
    public WebResult<String> testDownStop(int model) {
        assemblyBoltService.plcControlMotorJobRun(model, 12.0f, false, false);
        controlMicroMotorRun(1,4);
        return WebResult.okResult("PLC control motor is " + model + ",job run speed is " + 12);
    }

    @GetMapping("/controlPlcDORun")
    public WebResult<String> controlPlcDORun(int bit, boolean open) {
        assemblyBoltService.writeMotorIOStatus(bit, open ? 1 : 0, new CommonCallback<Boolean>() {
            @Override
            public void statusCallback(Boolean aBoolean) {

            }
        });
        return WebResult.okResult("inspire motor's salved is " + bit + "status is " + open);
    }

    /**
     * 因时 串口 7
     * micro 串口 3
     */
    @GetMapping("/controlInspireMotorRun")
    public WebResult<String> controlInspireMotorRun(int instruct) {
        assemblyBoltService.controlInspireMotorRun("7", instruct, s -> {
            log.info("电机接收指令完成");
        });
        return WebResult.okResult("inspire motor's salved is " + instruct);
    }

    @GetMapping("/controlMicroMotorRun")
    public WebResult<String> controlMicroMotorRun(int model, int instruct) {
        assemblyBoltService.controlMicroMotorRun("3", model, instruct, s ->
                log.info("电机接收指令完成"));
        return WebResult.okResult("Micro motor's salved is " + instruct);
    }


}
